package com.labot.webapp.action;

import java.util.Date;
import java.util.List;

import org.springframework.security.core.context.SecurityContextHolder;

import com.labot.model.Centro;
import com.labot.model.Envio;
import com.labot.types.AuditEventType;
import com.labot.types.AuditType;


/**
 * Action for facilitating User Management feature.
 */
public class CentroAction extends BaseAction {
    private static final long serialVersionUID = 6776548938712115191L;
    
    private List<Centro> centros;
   
    private String idcentro;
    private String nombre;
    private String origen;
    private String mail;

    public String execute() {
        return SUCCESS;
    }
    
    public String cancel() {
    	return CANCEL;
    }


    /**
     * Fetch all Configs from database and put into local "configs" variable for retrieval in the UI.
     *
     * @return "success" if no exceptions thrown
     */
    public String list() {
    		
    	centros = centroManager.getCentros();
    	
    	return SUCCESS;
    }
    
    public String edit(){
    	if(idcentro != null && !idcentro.equals("")){
    		Centro c = centroManager.buscarCentro(Long.parseLong(idcentro));
    		nombre = c.getNombre();
    		origen = String.valueOf(c.getOrigen());
    		mail = c.getMail();
    	}
    	return SUCCESS;
    }
    
    public String save(){
    	Centro c = null;
    	if(idcentro != null && !idcentro.equals("")){
    		c = centroManager.buscarCentro(Long.parseLong(idcentro));
    	}else{
    		c = new Centro();
    	}
    	c.setNombre(nombre);
    	c.setOrigen(Integer.parseInt(origen));
    	c.setMail(mail);
    	try{
    	centroManager.save(c);
    	}catch(Exception e){
    		super.addActionError("Error al cargar el centro!");
    		e.printStackTrace();
    		return ERROR;
    	}
    	super.addActionMessage("Centro cargado correctamente!");
    	auditDao.log(AuditEventType.ACTION_STRUTS, AuditType.INFO, this.getClass().getSimpleName() , SecurityContextHolder.getContext().getAuthentication().getName(), "Se edito o creo el centro " + c.getNombre());
    	return SUCCESS;
    	
    }
    
    public String remove(){
    	Centro c = null;
    	try{
    		c = centroManager.buscarCentro(Long.parseLong(idcentro));
    		c.delete();
    		centroManager.save(c);
    	}catch(Exception e){
    		super.addActionError("Error al borrar el centro!");
    		return SUCCESS;
    	}
    	super.addActionMessage("Se ha borrado el centro!");
    	auditDao.log(AuditEventType.ACTION_STRUTS, AuditType.WARNING, this.getClass().getSimpleName() , SecurityContextHolder.getContext().getAuthentication().getName(), "Se borro el centro " + c.getNombre());
    	return SUCCESS;
    	
    }

	public List<Centro> getCentros() {
		return centros;
	}

	public void setCentros(List<Centro> centros) {
		this.centros = centros;
	}

	public String getIdcentro() {
		return idcentro;
	}

	public void setIdcentro(String idcentro) {
		this.idcentro = idcentro;
	}

	public String getNombre() {
		return nombre;
	}

	public void setNombre(String nombre) {
		this.nombre = nombre;
	}

	public String getOrigen() {
		return origen;
	}

	public void setOrigen(String origen) {
		this.origen = origen;
	}

	public String getMail() {
		return mail;
	}

	public void setMail(String mail) {
		this.mail = mail;
	}
    
	
	
	

}
